package com.twu.biblioteca;

import java.util.List;

/**
 * Created by dmortime on 07/03/2014.
 */
public class CheckOutBookMenu implements IMenu {
    private OutputWriter outputWriter;
    private InputReader inputReader;
    private Library library;
    private Customer customer;

    public CheckOutBookMenu(OutputWriter outputWriter, InputReader inputReader, Library library, Customer customer) {
        this.outputWriter = outputWriter;
        this.inputReader = inputReader;
        this.library = library;
        this.customer = customer;
    }

    public void display() {
        outputWriter.writeLines("We have the following books available:");
        for(Book book : library.availableBooks()) {
            outputWriter.writeLines(book.getTitle());
        }
        outputWriter.writeLines("Enter the title of the book you wish to borrow:");
    }

    public IMenu nextMenu() {
        String title = inputReader.getInput();
        List<Book> matchingBooks = library.getAvailableBooksByTitle(title);
        if (matchingBooks.size() > 0) {
            Book theBook = matchingBooks.get(0);
            library.loanBook(theBook);
            customer.receiveBook(theBook);
            outputWriter.writeLines("Thank you! Enjoy the book!");
        } else {
            outputWriter.writeLines("That book is not available.");
        }
        return new MainMenuWithLoggedInCustomer(outputWriter, inputReader, library, customer);
    }
}
